﻿using Sasa.QualityTools.CodeAnalysis.Core.Requests;
using System;
using System.Collections.Generic;
using System.IO;

namespace Sasa.QualityTools.CodeAnalysis.Core.Services.CodeAnalysis
{
    internal class AssemblyAnalyzer : CodeAnalyzerBase
    {
        private AssemblyAnalysisRequest request;

        private DefaultFxCopReportValueConverter converter;


        protected internal override IFxCopReportValueConverter FxCopReportValueConverter
        {
            get
            {
                return converter;
            }
        }


        internal AssemblyAnalyzer(AssemblyAnalysisRequest request)
            : base(request)
        {
            this.request = request;
            converter = new DefaultFxCopReportValueConverter();
        }


        protected internal override IEnumerable<string> CreateFxCopCmdOptions()
        {
            if (!File.Exists(request.AssemblyPath)) throw new InvalidOperationException("The specified assembly (" + request.Target + ") doesn't exist.");

            var options = new List<string>();
            options.Add("/f:" + '"' + request.AssemblyPath + '"');
            options.Add("/o:" + '"' + OutputPath + '"');
            options.Add("/c");
            options.Add("/q");
            options.Add("/to:" + ((request.Timeout > 0) ? request.Timeout : 0));
            if (request.EnableSearchGac) options.Add("/gac");
            if (request.IgnoreGeneratedCode) options.Add("/igc");
            if (File.Exists(request.RuleSetPath)) options.Add("/rs:=" + '"' + request.RuleSetPath + '"');
            return options;
        }
    }
}
